package com.moli.iov.down.command.mq.activemq.handle;

import com.moli.iov.down.model.dto.remoting.CommandDto;
import com.moli.iov.down.command.design.command.retry.RetryCommand;
import com.moli.iov.down.command.design.factory.RetryCommandFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Component;

/**
 * <p>
 *  延时消息处理
 * </p>
 *
 * @author: zql
 * @Date: 2018/11/27 10:40
 */
@Component
public class MessageHandler {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private TaskExecutor taskExecutor;

    /**
     * 指令延时消息业务处理
     * @param commandDto
     */
    public void handle(CommandDto commandDto){
        logger.info("[补发命令] 参数：{}", commandDto);
        taskExecutor.execute(()->{
            try{
                RetryCommand retryCommand = RetryCommandFactory.getRetryCommand(commandDto.getCommand());
                if(retryCommand == null){
                    logger.error("[补发命令]  errormsg: 未定义指令  数据：{}", commandDto);
                    return;
                }
                retryCommand.handle(commandDto);
            }catch(Exception e){
                logger.error("延时消息处理 线程中处理异常", e);
            }

        });
    }
}
